package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImMessage;
import com.yuyh.library.utils.TimeUtil;
import com.zq.utils.StringUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by leijiawen on 2017/7/4.
 */
public class AccountAdapter extends BaseRecycleViewAdapter {
    private Context context;
    private List<ImMessage> imMessages = new ArrayList<>();
    private LayoutInflater inflater;

    public AccountAdapter(Context context, List<ImMessage> imMessages) {
        this.context = context;
        this.imMessages = imMessages;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (holder == null) {
            View view = inflater.inflate(R.layout.layout_account_item, parent, false);
            holder = new MyViewHolder(view);
            return holder;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ImMessage imMessage = imMessages.get(position);
        Integer messageType = imMessage.getMessageType();
        String content = imMessage.getContent();
        String fromname = imMessage.getFromname();
        Long receiveTime = imMessage.getSendTime();
        String recentlyTime = TimeUtil.getRecentlyTime(receiveTime);
        MyViewHolder holder1 = (MyViewHolder) holder;
        holder1.tvTime.setText(recentlyTime);
        holder1.tvTimes.setText(recentlyTime);
        holder1.tvName.setText(fromname);
        if (messageType == Constant.MESSAGE_TYPR_TRANSFOR_ISCOME) {
            holder1.tvType.setText(context.getResources().getString(R.string.get_money));
            holder1.tvTitle.setText(context.getResources().getString(R.string.get_money_e1));
            holder1.tvTitle2.setText(context.getResources().getString(R.string.pay_money_person));
            holder1.tvLiean3.setVisibility(View.GONE);
        } else if (messageType == Constant.NOTICE_OF_MONEY_ADD_FOR_JOIN_GROUP) {

            holder1.tvType.setText(context.getResources().getString(R.string.get_money));
            holder1.tvTitle.setText(context.getResources().getString(R.string.get_money_e1));
            holder1.tvTitle2.setText(context.getResources().getString(R.string.pay_money_person2));
            holder1.tvLiean3.setVisibility(View.VISIBLE);
        } else if (messageType == Constant.NOTICE_OF_MONEY_REDUCE_JOIN_GROUP) {

            holder1.tvType.setText(context.getResources().getString(R.string.pay_money));
            holder1.tvTitle.setText(context.getResources().getString(R.string.pay_money_e));
            holder1.tvTitle2.setText(context.getResources().getString(R.string.pay_money_person2));
            holder1.tvLiean3.setVisibility(View.VISIBLE);
        } else {
            holder1.tvType.setText(context.getResources().getString(R.string.pay_money));
            holder1.tvTitle.setText(context.getResources().getString(R.string.pay_money_e));
            holder1.tvTitle2.setText(context.getResources().getString(R.string.pay_money_person));
            holder1.tvLiean3.setVisibility(View.GONE);
        }
        String amt = null;
        String groupName = null;
        try {
            JSONObject ojb = new JSONObject(content);

            if (messageType == Constant.NOTICE_OF_MONEY_ADD_FOR_JOIN_GROUP
                    || messageType == Constant.NOTICE_OF_MONEY_REDUCE_JOIN_GROUP) {
                amt = ojb.getString("fee");
            } else {
                amt = ojb.getString("amt");
            }
            groupName = ojb.getString("groupName");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        holder1.tvAmt.setText(String.format("￥%s", amt));
        holder1.tvExplain.setText(StringUtil.format(context, R.string.pay_for_join_group, groupName, amt));
    }

    @Override
    public int getItemCount() {
        return imMessages.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tvpinzheng)
        TextView tvpinzheng;
        @BindView(R.id.tv_times)
        TextView tvTimes;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_amt)
        TextView tvAmt;
        @BindView(R.id.tv_title2)
        TextView tvTitle2;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_liean2)
        LinearLayout tvLiean2;
        @BindView(R.id.tv_title3)
        TextView tvTitle3;
        @BindView(R.id.tv_explain)
        TextView tvExplain;
        @BindView(R.id.tv_liean3)
        LinearLayout tvLiean3;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
